/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package slicktemplate;

import java.util.ArrayList;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.Sound;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.state.StateBasedGame;

/**
 *
 * @author andrew
 */
public class TurretSlowButton extends Collideable implements Updateable, Renderable {

    float x, y;
    public static final ArrayList<TurretSlowButton> all = new ArrayList<TurretSlowButton>();
    Image sprite;
    private boolean lastPressed = true;
    Sound[] noMoneySounds = new Sound[7];
    static final int COST = 100;
    static final String NAME = "Poison Tower";

    public TurretSlowButton(float inx, float iny) {
        all.add(this);
        x = inx;
        y = iny;
        try {
            sprite = new Image("sprites/turretSlow.png"); //try to load the sprite
            noMoneySounds[0] = new Sound("sounds/nocash0.ogg");
            noMoneySounds[1] = new Sound("sounds/nocash1.ogg");
            noMoneySounds[2] = new Sound("sounds/nocash2.ogg");
            noMoneySounds[3] = new Sound("sounds/nocash3.ogg");
            noMoneySounds[4] = new Sound("sounds/nocash4.ogg");
            noMoneySounds[5] = new Sound("sounds/nocash5.ogg");
            noMoneySounds[6] = new Sound("sounds/nocash6.ogg");
        } catch (Exception e) {
            e.printStackTrace();
        }
        boundingBox = new Rectangle(x, y, sprite.getWidth() - 1, sprite.getHeight() - 1);
    }

    @Override
    public boolean update(Input input, StateBasedGame sb) {
        if (input.isMouseButtonDown(input.MOUSE_LEFT_BUTTON)) {
            if (!lastPressed) {
                lastPressed = true;
                if (getCollision(input.getMouseX(), input.getMouseY())) {
                    if (Game.money >= COST) {
                        Game.money -= COST;
                        new TurretSlowPlacer(x, y);
                    } else {
                        int index = (int)(Math.random()*7);
                        noMoneySounds[index].play();
                    }
                }
            }
        } else {
            lastPressed = false;
        }

        return false;
    }

    @Override
    public void render(Graphics g) {
        sprite.draw((float) x, (float) y);
        g.drawString(NAME+": " + COST, x+40, y);
    }

    public void mousePressed(StateBasedGame sb, int button) {
    }

    @Override
    public float getX() {
        return x;
    }

    @Override
    public float getY() {
        return y;
    }

    @Override
    boolean isBuilding() {
        return true;
    }
}
